package com.valenx.distributedmulticamerasystem.camera.view;


import com.valenx.distributedmulticamerasystem.camera.CameraController;
import com.valenx.distributedmulticamerasystem.camera.utils.LogUtil;
import ohos.agp.components.Attr;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.SurfaceOps;
import ohos.agp.render.Canvas;
import ohos.app.Context;

import java.util.Optional;

public class CameraView extends SurfaceProvider {
    private final CameraController cameraController;//相机控制器
//=============================================================================================================

    //CameraView构造器（1参）
    public CameraView(Context context) {
        this(context, null);
    }
//-------------------------------------------------------------------------------------------------------------

    //CameraView构造器（2参）
    public CameraView(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }
//-------------------------------------------------------------------------------------------------------------

    //CameraView构造器（3参）
    public CameraView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);

        boolean isFront = false;//前后相机标志

        //获取布局中设置的前后相机标志
        if (attrSet != null) {
            Optional<Attr> optIsFront = attrSet.getAttr("front_camera");
            if (optIsFront.isPresent()) {
                isFront = "true".equals(optIsFront.get().getStringValue());
            }
        }

        cameraController = new CameraController(mContext, isFront,this);//创建相机控制器
        initView();//初始化CameraView（SurfaceProvider）
    }
//=============================================================================================================

    //初始化CameraView（SurfaceProvider）
    private void initView() {
        setVisibility(Component.VISIBLE);//设置可见
        setFocusable(Component.FOCUS_ENABLE);//设置可聚焦
        setTouchFocusable(true);//设置触摸模式
        pinToZTop(false);//画布装载器置顶：false
        if (getSurfaceOps().isPresent()) {
            getSurfaceOps().get().addCallback(new SurfaceCallback()); //添加画布回调实例
        }

        //人脸识别框
        this.addDrawTask(new Component.DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                if (cameraController.lines != null && cameraController.lines.length != 0) {
                    //将画布坐标移动到屏幕中心
                    canvas.translate(CameraView.this.getWidth() >> 1, CameraView.this.getHeight() >> 1);
                    //绘制矩形框
                    canvas.drawLines(cameraController.lines, cameraController.paint);
                } else {
                    canvas.clipRect(cameraController.faceRect);
                }
            }
        });

        LogUtil.info("CameraView:", "initSurface()-完成");
    }
//************************************************************************************************************

    //画布回调接口的实现类
    private class SurfaceCallback implements SurfaceOps.Callback {

        //画布创建时回调
        @Override
        public void surfaceCreated(SurfaceOps surfaceOps) {
            if (surfaceOps != null) {
                surfaceOps.setFixedSize(getHeight(), getWidth());//固定画布装载器提供的画布宽高与画布装载器的宽高一致
            }
            LogUtil.info("CameraView:", "surfaceCallback-surfaceCreated()-预览画布创建，绑定到相机控制器");
            cameraController.bind(CameraView.this);//将预览界面、宽高绑定给相机控制器
        }

        //画布改变时回调
        @Override
        public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {

        }

        //画布销毁时回调
        @Override
        public void surfaceDestroyed(SurfaceOps surfaceOps) {
            cameraController.releaseCamera();//释放相机设备
        }
    }
//************************************************************************************************************

    //获取相机控制器
    public CameraController getCameraController() {
        return cameraController;
    }
}
